package com.vipheyue.huaxue.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.vipheyue.huaxue.R;
import com.vipheyue.huaxue.adapter.BaseRecyclerAdapter;
import com.vipheyue.huaxue.adapter.BaseRecyclerViewHolder;
import com.vipheyue.huaxue.bean.MessageBean;
import com.vipheyue.huaxue.bean.MyUser;
import com.vipheyue.huaxue.bean.ThingBean;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

public class BrowseMessageActivity extends AppCompatActivity {
    @Bind(R.id.recycler)
    RecyclerView recyclerView;
    List<MessageBean> mdata = new ArrayList<MessageBean>();
    private BaseRecyclerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_message);
        ButterKnife.bind(this);
        initview();
        initData();
    }

    private void initData() {
//        String thingBeanId=getIntent().getStringExtra("thingBeanId");
        BmobQuery<MessageBean> query = new BmobQuery<MessageBean>();
        ThingBean thingBean= new ThingBean();
//        thingBean.setObjectId(thingBeanId);
        query.include("sendBy");// 希望在查询帖子信息的同时也把发布人的信息查询出来
//        query.addWhereEqualTo("thingbean",thingBean );    // 查询和东西相关的报价
        MyUser user = BmobUser.getCurrentUser(this, MyUser.class);
        if (user == null) {
            startActivity(new Intent(this,LoginActivity.class));
            return;
        }
        query.addWhereEqualTo("sendTo",user );
        query.findObjects(this, new FindListener<MessageBean>() {
            @Override
            public void onSuccess(List<MessageBean> list) {
                setData(list);
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(BrowseMessageActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void setData(List<MessageBean> bean) {

        if (bean == null) {
            Toast.makeText(this, "没有查询到数据,请重新启动", Toast.LENGTH_SHORT).show();
            return;
        }
        mdata.addAll(bean);
        adapter.notifyDataSetChanged();
    }

    private void initview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);//设置显示方式
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());// 第一 普通
        adapter = new BaseRecyclerAdapter<MessageBean>(this, mdata, R.layout.activity_item) {
            @Override
            public void init(BaseRecyclerViewHolder holder, MessageBean bean, int position) {
                holder.setText(R.id.tv_item, bean.getContent() + "   "+bean.getSendBy().getMobilePhoneNumber());
            }
        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(BrowseMessageActivity.this, DetailActivity.class);
                intent.putExtra("uri", mdata.get(position));
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);//设置适配器
    }


}


